
import { draw_findById } from '@/api/draw'

const draw = {
  state: {
    percent: 0, //面板百分比
    cacheForm: {},//缓存创建抽奖数据
    cacheDrawItems: [],//缓存模板数据
    checkDraw: ''
  },

  mutations: {
    SET_PERCENT_NUM: (state, num) => {
      state.percent = num
    },
    SET_FORM_DATA: (state, data) => {
      state.cacheForm = data
    },
    SET_DRAW_ITEMS: (state, list) => {
      state.cacheDrawItems = list
    },
    SET_CHECK_DRAW: (state, name) => {
      state.checkDraw = name
    }
  },

  actions: {
    // 累加百分比
    async SetPercentNum({ commit }, num) {
      commit('SET_PERCENT_NUM',num)
    },
    async SetDrawFormData({ commit }, args) {
        const { data } = await draw_findById({id: args.id});
        if(data.status = 100){
          const result = data.data;
          const params = {
            date: [result.active_starttime,result.active_endtime],
            everyday: !!result.everyday_status,
            everyone: !!result.everyone_status,
          }
          const insert = {...result, ...params}
          commit("SET_FORM_DATA", insert)
          commit("SET_DRAW_ITEMS",insert.drawItems)
        }
    },
    async ClearDrawFormData({ commit }) {
      commit("SET_FORM_DATA", {})
      commit("SET_DRAW_ITEMS",[])
    },
    async SetCheckDraw({ commit }, name) {
      commit('SET_CHECK_DRAW', name);
    }
  }
}

export default draw
